package com.wgts.wgts.mvp.view.activity.mycenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.netmodle.resp.CollectListEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.graphics.Paint.STRIKE_THRU_TEXT_FLAG;

/*
 *
 *
 *   data:2018/9/2911:33
 *
 */public class CollectRecycleAdapter extends RecyclerView.Adapter<CollectRecycleAdapter.ViewHold> {
    private Context context;
    private List<CollectListEntity.List> list;
    private cancelClickListener cancelClickListener;

    public CollectRecycleAdapter(Context context, List<CollectListEntity.List> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate(R.layout.collect_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHold viewHold, final int i) {
        if (list.get(i).status.equals("ACTIVE")) {
            viewHold.shopStatusLy.setVisibility(View.GONE);
            viewHold.cancelCollectLy.setVisibility(View.VISIBLE);
            viewHold.goBuyLy.setBackgroundResource(R.drawable.time_action_bg);
            viewHold.goBuyText.setText("立即抢购");
            viewHold.shoppingCarPrice.setTextColor(ContextCompat.getColor(context, R.color.title_text));
        } else {
            viewHold.shopStatusLy.setVisibility(View.VISIBLE);
            viewHold.cancelCollectLy.setVisibility(View.GONE);
            viewHold.goBuyLy.setBackgroundResource(R.drawable.nike_name_border);
            viewHold.goBuyText.setText("已下架");
            viewHold.shoppingCarPrice.setTextColor(ContextCompat.getColor(context, R.color.hint_6));
        }
        if (AppInfor.getInstance().getUserState() < 2){
            viewHold.marketPrice.setTextColor(ContextCompat.getColor(context, R.color.hint_6));
            viewHold.marketPrice.setText("￥" + list.get(i).marketPrice);
            viewHold.marketPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        }else {
            viewHold.marketPrice.setTextColor(ContextCompat.getColor(context, R.color.red_bg));
            String price = "赚" + list.get(i).marketPrice;
            Spannable span = new SpannableString(price);
            span.setSpan(new AbsoluteSizeSpan(12, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            span.setSpan(new AbsoluteSizeSpan(17, true), 1, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            viewHold.marketPrice.setText(span);
        }
        MoHuGlide.getInstance().topRaduice(context, list.get(i).productSmallImage, viewHold.shoppingCarItemImage);
        viewHold.shoppingCarPrice.setText("￥" + list.get(i).retailPrice);
        viewHold.sallNumber.setText("已售" + list.get(i).sellCount);
        viewHold.cancelCollectLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cancelClickListener != null){
                    cancelClickListener.cancelClick(viewHold.cancelCollectLy, i);
                }
            }
        });

        viewHold.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(context, JDGoodinfoActivity.class);
                    intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                    intent.putExtra("id", list.get(i).productId);
                    if (!(context instanceof Activity)) {
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    }
                    context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.shopping_car_item_image)
        ImageView shoppingCarItemImage;
        @BindView(R.id.shopping_car_title)
        TextView shoppingCarTitle;
        @BindView(R.id.shopping_car_price)
        TextView shoppingCarPrice;
        @BindView(R.id.market_price)
        TextView marketPrice;
        @BindView(R.id.sall_number)
        TextView sallNumber;
        @BindView(R.id.go_buy_text)
        TextView goBuyText;
        @BindView(R.id.go_buy_ly)
        RelativeLayout goBuyLy;
        @BindView(R.id.cancel_collect_ly)
        RelativeLayout cancelCollectLy;
        @BindView(R.id.shop_status_ly)
        RelativeLayout shopStatusLy;
        private View itemView;

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    public interface cancelClickListener{
        void cancelClick(View view, int position);
    }

    public void setCancelClickListener(cancelClickListener cancelClickListener){
        this.cancelClickListener = cancelClickListener;
    }
}
